import pygame, sys, math, random
 
pygame.init()
pygame.joystick.init()

joysticks = [pygame.joystick.Joystick(x) for x in range(pygame.joystick.get_count())]
for joystick in joysticks:
    joystick.init()
    print joystick.get_name()

from Milk import Milk
from Glass import Glass
from ScoreBoard import ScoreBoard
from Enlarge import Enlarge

clock = pygame.time.Clock()

width = 1000
height = 700
size = width, height

screen = pygame.display.set_mode(size)
startImage = pygame.image.load("Resources/Cow/cow.png")
startImage = pygame.transform.scale(startImage, size)
startRect = startImage.get_rect()
bgImage = pygame.image.load("Resources/grass.png")
bgRect = bgImage.get_rect()

score = ScoreBoard([width-100, 75])
bgColor = r,g,b = 0,0,0
     
glass = Glass([10,5], [25,50], [int(width/2),(height*3/3.5)])

drops = []

powerUps = []             
            
start = False
while True:
    while not start:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN:
                    start = True
            if event.type == pygame.JOYBUTTONDOWN:
                print event.button
                if event.button == 0:
                    start = True
        
        screen.blit(startImage,startRect)
        pygame.display.flip()
        clock.tick(60)
    
    bgImage = pygame.image.load("Resources/grass.png")
    
    level =1000000000000000000000                                       
   
    while start:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                    glass.direction("right")
                if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                    glass.direction("left")
            if event.type == pygame.KEYUP:
            
                if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                    glass.direction("stop right")
                if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                    glass.direction("stop left")
            if joysticks >= 1:
                if event.type == pygame.JOYAXISMOTION:
                    print event.joy, event.axis, event.value

        if random.randint(0,800) == 0: 
            powerUps += [Enlarge([random.randint(-5,5), random.randint(1,5)],
                                 [random.randint(25, width-25), 0])]
                    
        
        if random.randint(0,75) == 0:
            drops += [Milk([random.randint(-5,5), random.randint(1,5)], 
              [25, 50], 
              [random.randint(75, width-75), 0])]
        
        for milk in drops:
            milk.update()
        for powerUp in powerUps:
            powerUp.update()
            powerUp.collideWall(width,height)
            glass.collidePowerup(powerUp)
            
        glass.update()        
        
        for milk in drops:
            if milk.collideWall(width, height):
                score.scoreOutGlass()
        glass.collideWall(width, height)
        
        
        for milk in drops:
            if glass.collideBall(milk):
                score.scoreInGlass()
            
            
        for milk in drops:
            if not milk.living:
                drops.remove(milk)
                
                
        for powerUp in powerUps:
            if not powerUp.living:
                powerUps.remove(powerUp)        
               
        
                          
        screen.blit(bgImage, bgRect)
        screen.blit(glass.image, glass.rect)
        for milk in drops:
            screen.blit(milk.image, milk.rect)
        for powerUp in powerUps:
            screen.blit(powerUp.image, powerUp.rect)
        screen.blit(score.image, score.rect)
        pygame.display.flip()
        clock.tick(60)
